import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jufei/global/config.dart';
import 'package:jufei/jufei.dart';

class SplishView extends StatefulWidget {
  final SplashCallBack? splashCallBack;
  final BuildContext? context;
  const SplishView({ Key? key ,this.splashCallBack,this.context}) : super(key: key);

  @override
  _SplishViewState createState() => _SplishViewState();
}

class _SplishViewState extends State<SplishView> {

  MethodChannel? _channel;
  @override
  Widget build(BuildContext context) {
    if(defaultTargetPlatform == TargetPlatform.android){
      return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: AndroidView(
        viewType: Config.splishPage,
        creationParams:{},
        onPlatformViewCreated: (int id){
          _channel = MethodChannel("${Config.splishPage}_$id");
          _channel?.setMethodCallHandler((call) async{
            switch (call.method) {
              case "onError":
                  if(widget.splashCallBack != null){
                    widget.splashCallBack?.error!(call.arguments);
                  }
                break;
              case "onTimeOut":
                  if(widget.splashCallBack != null){
                    widget.splashCallBack?.timeOut!();
                  }
                break;
              case "onAdShow":
                  if(widget.splashCallBack != null){
                    widget.splashCallBack?.adShow!();
                  }
                break;
              case "onAdCanceled":
                  if(widget.splashCallBack != null){
                    widget.splashCallBack?.adCanceled!();
                  }
                break;
              case "onAdClicked":
                  if(widget.splashCallBack != null){
                    widget.splashCallBack?.adClicked!();
                  }
                break;
              default:
            }
          });
        },
        creationParamsCodec: const StandardMessageCodec(),
        ));
    }else{
      return Container();
    }
   
  }
}